/**
 * Created by garfield on 8/24/16.
 */
var mainApp = angular.module("mainApp",[]);
mainApp.controller("MerchantController",function ($scope, $http) {
    $scope.merchant = {};
    $scope.star = [];

    var req = {
        method:"GET",
        url:"/CHa/merchant"
    };
    $http(req)
        .then(function (res) {
            $scope.merchant = res.data;
            for(var i = 0;i<$scope.merchant.star;i++){
                $scope.star.push(i);
            }
        });
}).controller("CartController",function($scope,$rootScope,$http){

    var total = 0;
    var num = 0;
    $scope.ca = "";
    $scope.showList = false;

    $rootScope.user = [];

    $scope.carts = [];
    $scope.total = total;
    $scope.num = num;
    $scope.dishes = [];
    $scope.address = [];

    // Find dishes
    var req = {
        method:"GET",
        url:"/CHa/dishes?recommend=3"
    }
    $http(req)
        .then(function (res) {
            $scope.dishes = res.data;
        });

    //Find addresses
    var req = {
        method:"GET",
        url:"/CHa/session"
    }
    $http(req)
        .then(function (res) {
            var inreq = {
                method:"GET",
                url:"/CHa/user/"+res.data.uid
            }
            $http(inreq)
                .then(function (res) {
                    $rootScope.user = res.data;
                    $scope.address = $rootScope.user.address;
                });
        });

    //Find carts
    var req = {
        method:"GET",
        url:"/CHa/cart"
    }

    $http(req)
        .then(function (res) {
            $scope.carts = res.data;
        });

    $scope.addToCart = function(index){

        var exist = $scope.carts.some(function (o, inx, ary) {
            t = inx;
            return angular.equals(o.item,$scope.dishes[index]);
        });
        if(!exist){
            var cartItem = {item:$scope.dishes[index],count:1};
            $scope.carts.push(cartItem);
        }else{
            $scope.carts[t].count+=1;
        }
    };

    $scope.addDish = function (index) {
        $scope.carts[index].count++;
    }

    $scope.deleteDish = function (index) {
        $scope.carts[index].count--;
        if($scope.carts[index].count<=0){
            $scope.carts.splice(index,1);
        }
    }

    $scope.$watch("carts",function(n,o){
        $scope.total = 0;
        $scope.num = 0;
        $scope.carts.forEach(function (c) {
            $scope.total+=(c.item.price*c.count);
            $scope.num+=c.count;
        });

        //Update carts
        var req = {
            method:"PUT",
            url:"/CHa/cart",
            data:$scope.carts,
            headers : {
                "Content-Type":"application/json"
            }
        }

        $http(req)
            .then(function (res) {

            });
    },true);

    // Modify address
    $scope.editAddress = function (index) {
        $scope.ca = angular.copy($scope.address[index]);
    }
    
    $scope.addAddress = function () {
        $scope.ca.priority = 0;
        $scope.address.push($scope.ca);
        var req = {
            method:"PUT",
            url:"/CHa/user/"+$rootScope.user._id,
            // url:"/CHa/user/bcf730862544a4118edf48",
            data:{address:$scope.address},
            headers : {
                "Content-Type":"application/json"
            }
        };

        $http(req).then(function (res) {
            $scope.address = res.data.address;
        });
    }

    $scope.deleteAddress = function (index) {
        $scope.address.splice(index,1);
        var req = {
            method:"PUT",
            url:"/CHa/user/"+$rootScope.user._id,
            data:{address:$scope.address},
            headers : {
                "Content-Type":"application/json"
            }
        }
        $http(req).then(function (res) {
            $scope.address = res.data.address;
        });
    }

    $scope.check = function(){
        if($scope.carts.length>0){
            if($rootScope.isLogin){
                $scope.showList = true;
            }else{
                window.location.href = "login.html";
            }
        }
    }

    $scope.exitList = function () {
        $scope.showList = false;
    }

    $scope.submitOrder = function () {
        if($scope.carts.length>0){
            if($scope.ca.address){
                var odishes = [];
                $scope.carts.forEach(function (dish, index, arry) {
                    dish.item.num = dish.count;
                    odishes.push(dish.item);
                });

                var order = {
                    total:$scope.total,
                    taken:0,
                    dishes:odishes,
                    score:null,
                    comments:null,
                    address:$scope.ca.address
                };
                var req = {
                    method:"POST",
                    url:"/CHa/order",
                    data:order,
                    headers : {
                        "Content-Type":"application/json"
                    }
                }
                $http(req).then(function (res) {
                    $scope.carts = [];
                    $scope.showList = false;
                });
            }else{
                alert("Address cannot be blanked.");
            }
        }

    }
});
mainApp.controller('userCtrl',function ($scope,$rootScope,$http) {
    var req = {
        method:"GET",
        url:"/CHa/session"
    }

    $http(req)
        .then(function (res) {
            if(res.data.account!=undefined){
                $scope.userAccount = res.data.account;
                $scope.hasLogin = true;
                $rootScope.isLogin = true;
            }else{
                $scope.hasLogin = false;
                $rootScope.isLogin = false;
            }


        });

    $scope.logOut = function () {
        var clearSeesionReq = {
            url:"/CHa/session",
            method:"PUT"
        }
        $http(clearSeesionReq).then(function (res) {
            window.location.href = "home.html";
        })

    }
});